# -*- coding: utf-8 -*-
import sys
import os
import io
default_log_file_name = sys.argv[0]+'.log'


class Logger(object):
    def __init__(self, filename=default_log_file_name, path='./'):
        # sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
        self.terminal = sys.stdout
        self.log = open(os.path.join(path, filename), 'a', encoding='utf8')

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)
        self.log.flush()

    def flush(self):
        self.log.flush()


def setup_print_log(logfilename=None, path=None):
    if logfilename is None:
        logfilename = default_log_file_name
    if path is None:
        path = './'
    sys.stdout = Logger(filename=logfilename, path=path)